package com.cjy.controller;

import com.cjy.pojo.ImageType;
import com.cjy.service.ImageTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("/admin")
public class PictureTypeController {

    @Autowired
    private ImageTypeService imageTypeService;

    @GetMapping("/imageType")
    public String tags(Model model){

        model.addAttribute("imageType",new ImageType());
        return "admin/picture-type-input";
    }

    @GetMapping("/imageTypeList")
    public String tagList(@PageableDefault(size = 8,sort = {"id"},direction = Sort.Direction.DESC)
                          Pageable pageable,Model model){
        Page<ImageType> imageTypePage = imageTypeService.listImageType(pageable);
        model.addAttribute("page", imageTypePage);
        return "admin/pictureTypeManager";
    }

    @PostMapping("/addImageType")
    public String addType(@Valid ImageType imageType , BindingResult result, RedirectAttributes attributes){
        ImageType imageByName = imageTypeService.getImageTypeByName(imageType.getName());
        if (imageByName!=null){
            result.rejectValue("name","nameError","该博客分类已存在，不能重复添加！");
        }
        // 后端验证数据是否为空
        if (result.hasErrors()){
            return "admin/picture-type-input";
        }
        ImageType it = imageTypeService.saveImageType(imageType);
        if (it==null){
            // 保存失败
            attributes.addFlashAttribute("message","新增失败");
        }else {
            attributes.addFlashAttribute("message","新增成功");
        }
        return "redirect:/admin/imageTypeList";
    }

    @PostMapping("/imageType/{id}")
    public String editType(@Valid ImageType imageType ,BindingResult result, @PathVariable Long id ,  RedirectAttributes attributes){
        ImageType imageTypeByName = imageTypeService.getImageTypeByName(imageType.getName());
        if (imageTypeByName!=null){
            // 给属性校验
            result.rejectValue("name","nameError","该图片分类已存在，不能重复添加！");
        }
        // 后端验证数据是否为空
        if (result.hasErrors()){
            return "admin/imageTypeList";
        }
        ImageType it = imageTypeService.saveImageType(imageType);
        if (it==null){
            // 保存失败
            attributes.addFlashAttribute("message","更新失败");
        }else {
            attributes.addFlashAttribute("message","更新成功");
        }
        return "redirect:/admin/imageTypeList";
    }

    @GetMapping("/imageType/{id}/input")
    public String input(@PathVariable Long id, Model model){
        model.addAttribute("imageType",imageTypeService.getImageType(id));
        return "admin/picture-type-input";
    }

    @GetMapping("/imageType/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        imageTypeService.deleteImageType(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/imageTypeList";
    }


}
